﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Util
{
    public static class DateTimeExtensions
    {
        /// <summary>
        /// Makes the time start of day.
        /// </summary>
        /// <param name="startTime">The start time.</param>
        public static DateTime StartOfDay(this DateTime dateTime)
        {
            return dateTime.Date;
        }

        /// <summary>
        /// Make the DateTime at the end of the day
        /// </summary>
        /// <param name="endTime"></param>
        public static DateTime EndOfDay(this DateTime endTime)
        {
            return endTime.Date.AddDays(1).AddTicks(-1);
        }


        private static GregorianCalendar _gc = new GregorianCalendar();
        /// <summary>
        /// Function get week number of current month
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int GetWeekOfMonth(this DateTime time)
        {
            DateTime first = new DateTime(time.Year, time.Month, 1);
            return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
        }

        /// <summary>
        /// Function get week number of current year
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static int GetWeekOfYear(this DateTime time)
        {
            return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
        }
        private static readonly DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static long ConvertToTimestamp(this DateTime value)
        {
            TimeSpan elapsedTime = value - Epoch;
            return (long)elapsedTime.TotalSeconds;
        }
    }
}
